<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>添加图片</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all">

  <link rel="stylesheet" href="/css/cropper.min.css">
  <link rel="stylesheet" href="/css/ImgCropping.css">
  <style>
    @media screen and (min-width: 490px) {
      .layui-form-item .layui-input-inline {
        width: 60%;
      }
    }
  </style>
</head>
<body>
<!--图片裁剪框 start-->
<div style="display: none" class="tailoring-container">
  <div class="black-cloth" onclick="closeTailor(this)"></div>
  <div class="tailoring-content">
    <div class="tailoring-content-one">
      <label title="上传图片" for="chooseImg" class="l-btn choose-btn">
        <input type="file" accept="image/jpg,image/jpeg,image/png" name="file" id="chooseImg" class="hidden" onchange="selectImg(this)">
        选择图片
      </label>
      <div class="close-tailoring"  onclick="closeTailor(this)">×</div>
    </div>
    <div class="tailoring-content-two">
      <img id="tailoringImg">
    </div>
    <div class="tailoring-content-three">
      <button class="l-btn cropper-reset-btn">复位</button>
      <button class="l-btn cropper-rotate-btn">旋转</button>
      <button class="l-btn cropper-scaleX-btn">换向</button>
      <button class="l-btn sureCut" id="sureCut">上传</button>
    </div>
  </div>
</div>
<!--图片裁剪框 end-->
<div class="layui-form" lay-filter="layuiadmin-form-useradmin" id="layuiadmin-form-useradmin" style="padding: 20px 0 0 0;">
  <div class="layui-form-item">
    <label class="layui-form-label">分类</label>
    <div class="layui-input-inline">
      <select name="pFlag" lay-verify="required" lay-filter="pflag">
        <option value="">请选择分类</option>
        <option value="1" ${flag==1?'selected':''}>景点详情</option>
        <option value="2" ${flag==2?'selected':''}>学校地图</option>
      </select>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">${flag==0?'-':flag==1?'景点':'学校'}</label>
    <div class="layui-input-inline">
      <select name="pVsid" lay-verify="required" lay-search>
        <option value="">${flag==0?'请先选择分类':flag==1?'请选择景点':'请选择学校'}</option>
        #if(flag==1)
        #for(view : list)
        <option value="${view.vId}" ${id==view.vId?'selected':''}>${view.vName}</option>
        #end
        #elseif(flag==2)
        #for(sch : list)
        <option value="${sch.sId}" ${id==sch.sId?'selected':''}>${sch.sName}</option>
        #end
        #end
      </select>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">图片</label>
    <div class="layui-input-inline">
      <input type="text" name="pFilename" id="pic" lay-verify="required" placeholder="请上传图片" autocomplete="off" class="layui-input" >
    </div>
    <button style="float: left;" type="button" class="layui-btn" id="layuiadmin-upload-useradmin">上传图片</button>
  </div>
  <div class="layui-form-item layui-hide">
    <input type="button" lay-submit lay-filter="LAY-front-submit" id="LAY-front-submit" value="确认">
  </div>
</div>

<script src="/layuiadmin/layui/layui.js"></script>
<script src="https://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<script src="/js/cropper.min.js"></script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'upload'], function(){
        var $ = layui.$
            ,form = layui.form
            ,upload = layui.upload ;


        form.on('select(pflag)', function(data){
            location.href='pictureAdd?flag='+data.value;
        });


    });
        //弹出框水平垂直居中
        (window.onresize = function () {
            var win_height = $(window).height();
            var win_width = $(window).width();
            if (win_width <= 768){
                $(".tailoring-content").css({
                    "top": (win_height - $(".tailoring-content").outerHeight())/2,
                    "left": 0
                });
            }else{
                $(".tailoring-content").css({
                    "top": (win_height - $(".tailoring-content").outerHeight())/2,
                    "left": (win_width - $(".tailoring-content").outerWidth())/2
                });
            }
        })();

        //弹出图片裁剪框
        $("#layuiadmin-upload-useradmin").on("click",function () {
            $(".tailoring-container").toggle();
        });
        //图像上传
        function selectImg(file) {
            if (!file.files || !file.files[0]){
                return;
            }
            var reader = new FileReader();
            reader.onload = function (evt) {
                var replaceSrc = evt.target.result;
                //更换cropper的图片
                $('#tailoringImg').cropper('replace', replaceSrc,false);//默认false，适应高度，不失真
            }
            reader.readAsDataURL(file.files[0]);
        }
        //cropper图片裁剪
        $('#tailoringImg').cropper({
            #if (flag==1)
            aspectRatio: 658/462,//默认比例
            #end
            preview: '.previewImg',//预览视图
            guides: false,  //裁剪框的虚线(九宫格)
            autoCropArea: 0.99,  //0-1之间的数值，定义自动剪裁区域的大小，默认0.8
            movable: false, //是否允许移动图片
            dragCrop: true,  //是否允许移除当前的剪裁框，并通过拖动来新建一个剪裁框区域
            movable: true,  //是否允许移动剪裁框
            resizable: true,  //是否允许改变裁剪框的大小
            zoomable: true,  //是否允许缩放图片大小
            mouseWheelZoom: true,  //是否允许通过鼠标滚轮来缩放图片
            touchDragZoom: true,  //是否允许通过触摸移动来缩放图片
            rotatable: true,  //是否允许旋转图片
            crop: function(e) {
                // 输出结果数据裁剪图像。
            }
        });
        //旋转
        $(".cropper-rotate-btn").on("click",function () {
            $('#tailoringImg').cropper("rotate", 45);
        });
        //复位
        $(".cropper-reset-btn").on("click",function () {
            $('#tailoringImg').cropper("reset");
        });
        //换向
        var flagX = true;
        $(".cropper-scaleX-btn").on("click",function () {
            if(flagX){
                $('#tailoringImg').cropper("scaleX", -1);
                flagX = false;
            }else{
                $('#tailoringImg').cropper("scaleX", 1);
                flagX = true;
            }
            flagX != flagX;
        });

        //裁剪后的处理
        $("#sureCut").on("click",function () {
            if ($("#tailoringImg").attr("src") == null ){
                layer.msg('请选择图片后上传');
                return false;
            }else{
                $("#sureCut").text("上传中...");
                //关闭裁剪框
                closeTailor();

                var cas = $('#tailoringImg').cropper('getCroppedCanvas');//获取被裁剪后的canvas
                var base64url = cas.toDataURL('image/jpeg'); //转换为base64地址形式
                uploadFile(encodeURIComponent(base64url),base64url)//编码后上传服务器

                $("#sureCut").text("上传");
            }
        });
        //关闭裁剪框
        function closeTailor() {
            $(".tailoring-container").toggle();
        }
        //ajax请求上传
        function uploadFile(file,base64url) {
            layer.msg('图片上传中...', {
                icon: 16
                ,shade: 0.01
            });
            $.ajax({
                url : '/admin/uploadpic',
                dataType:"json",
                type : 'POST',
                data : "file=" + file,
                async : false,
                success : function(data) {
                    if (data.status == 1){
                        layer.msg('上传成功', {
                            offset: '15px'
                            ,icon: 1
                            , time: 1000
                        });
                        $("#pic").val(data.src);
                        return true;
                    }
                    else {
                        layer.msg('上传失败', {
                            offset: '15px'
                            ,icon: 0
                            , time: 1000
                        });
                        return false;
                    }
                },
                error: function(data) {
                    console.log(data);
                    layer.msg("系统繁忙", {
                        offset: '15px'
                        ,time: 3000
                        ,icon:2
                        ,anim:6
                    });
                    return false;
                }
            });
        }

</script>
</body>
</html>